
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>对象 · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    <link rel="stylesheet" href="gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="prototype.html" />
    
    
    <link rel="prev" href="数组.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="./">
            
                <a href="./">
            
                    
                    Introduction
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="变量.html">
            
                <a href="变量.html">
            
                    
                    变量
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="解构.html">
            
                <a href="解构.html">
            
                    
                    解构
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="字符串.html">
            
                <a href="字符串.html">
            
                    
                    字符串
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="函数.html">
            
                <a href="函数.html">
            
                    
                    函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="数组.html">
            
                <a href="数组.html">
            
                    
                    数组
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.7" data-path="对象.html">
            
                <a href="对象.html">
            
                    
                    对象
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="prototype.html">
            
                <a href="prototype.html">
            
                    
                    prototype
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9" data-path="Promise.html">
            
                <a href="Promise.html">
            
                    
                    Promise
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10" data-path="模块化.html">
            
                <a href="模块化.html">
            
                    
                    模块化
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11" data-path="class和继承.html">
            
                <a href="class和继承.html">
            
                    
                    class和继承
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.12" data-path="this.html">
            
                <a href="this.html">
            
                    
                    this
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.13" data-path="Symbol和generator.html">
            
                <a href="Symbol和generator.html">
            
                    
                    Symbol和generator
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.14" data-path="async_await.html">
            
                <a href="async_await.html">
            
                    
                    async_await
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.15" data-path="Set和WeakSet.html">
            
                <a href="Set和WeakSet.html">
            
                    
                    Set和WeakSet
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.16" data-path="Map和WeakMap.html">
            
                <a href="Map和WeakMap.html">
            
                    
                    Map和WeakMap
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.17" data-path="数字和Math.html">
            
                <a href="数字和Math.html">
            
                    
                    数字和Math
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.18" data-path="正则.html">
            
                <a href="正则.html">
            
                    
                    正则
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.19" data-path="Proxy.html">
            
                <a href="Proxy.html">
            
                    
                    Proxy
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="." >对象</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="&#x5BF9;&#x8C61;">&#x5BF9;&#x8C61;</h1>
<p>&#x5BF9;&#x8C61;&#x7B80;&#x6D01;&#x8BED;&#x6CD5;</p>
<pre><code class="lang-js"><span class="hljs-comment">// Old</span>
&lt;script type=<span class="hljs-string">&quot;text/javascript&quot;</span>&gt;
    <span class="hljs-keyword">let</span> name = <span class="hljs-string">&apos;Strive&apos;</span>;
    <span class="hljs-keyword">let</span> age = <span class="hljs-number">18</span>;
    <span class="hljs-keyword">let</span> json = {
        name: name,
        age: age
    };
    <span class="hljs-built_in">console</span>.log(json.name, json.age);
<span class="xml"><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span></span>

<span class="hljs-comment">// New</span>
&lt;script type=<span class="hljs-string">&quot;text/javascript&quot;</span>&gt;
    <span class="hljs-keyword">let</span> name = <span class="hljs-string">&apos;Strive&apos;</span>;
    <span class="hljs-keyword">let</span> age = <span class="hljs-number">18</span>;
    <span class="hljs-keyword">let</span> json = {
        name, <span class="hljs-comment">//&#x76F4;&#x63A5;&#x4F7F;&#x7528;&#x5373;&#x53EF;</span>
        age
    };
    <span class="hljs-built_in">console</span>.log(json.name, json.age);
<span class="xml"><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span></span>
</code></pre>
<pre><code class="lang-js">&lt;script type=<span class="hljs-string">&quot;text/javascript&quot;</span>&gt;
    <span class="hljs-keyword">let</span> name = <span class="hljs-string">&apos;Strive&apos;</span>;
    <span class="hljs-keyword">let</span> age = <span class="hljs-number">18</span>;
    <span class="hljs-keyword">let</span> json = {
        name, <span class="hljs-comment">// &#x76F8;&#x5F53;&#x4E8E; name: name</span>
        age, <span class="hljs-comment">// &#x76F8;&#x5F53;&#x4E8E; age: age</span>
        <span class="hljs-comment">/**
        showA: function() {
            return this.name;
        }*/</span>
        <span class="hljs-comment">//&#x65B0;&#x5199;&#x6CD5;:</span>
        showA() { <span class="hljs-comment">// &#x76F8;&#x5F53;&#x4E8E; showA: function() ...</span>
            <span class="hljs-keyword">return</span> <span class="hljs-keyword">this</span>.name;
        },
        showB() {
            <span class="hljs-keyword">return</span> <span class="hljs-keyword">this</span>.age;
        },
        <span class="hljs-comment">/**
        //&#x8FD9;&#x91CC;&#x4E00;&#x5B9A;&#x4E0D;&#x8981;&#x4F7F;&#x7528;&#x7BAD;&#x5934;&#x51FD;&#x6570;
        showC:() =&gt; {
            return this.name;
        }*/</span>
    };
    <span class="hljs-built_in">console</span>.log(json.showA(), json.showB());
<span class="xml"><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span></span>
</code></pre>
<h3 id="objectis">Object.is()</h3>
<pre><code class="lang-js">&lt;script type=&quot;text/javascript&quot;&gt;
    console.log(NaN == NaN); //false
    console.log(Object.is(NaN, NaN)); //true
    console.log(+0 == -0); //true
    console.log(Object.is(+0, -0)); //false
&lt;/script&gt;
</code></pre>
<h3 id="objectassign-&#x5408;&#x5E76;&#x5BF9;&#x8C61;">Object.assign() &#x5408;&#x5E76;&#x5BF9;&#x8C61;</h3>
<pre><code class="lang-js">&lt;script type=&quot;text/javascript&quot;&gt;
    let json = {a:1};
    let json2 = {b:2, a:2};
    let json3 = {c:3};

    // Object.assign(&#x76EE;&#x6807;&#x5BF9;&#x8C61;, source1, source2, ...)
    let obj = Object.assign({}, json, json2, json3);
    console.log(obj); // {a: 2, b: 2, c: 3}
&lt;/script&gt;
</code></pre>
<pre><code class="lang-js">&lt;script type=&quot;text/javascript&quot;&gt;
    let json = {score:100};
    let defaults = {name:&apos;liu&apos;, score:0}
    // &#x4E00;&#x822C;&#x505A;&#x6CD5;: &#x628A;&#x9ED8;&#x8BA4;&#x53C2;&#x6570;&#x653E;&#x5728;&#x524D;&#x9762;,&#x628A;&#x670D;&#x52A1;&#x5668;&#x8FD4;&#x56DE;&#x7684;&#x6570;&#x636E;&#x653E;&#x5728;&#x540E;&#x9762;
    // &#x540E;&#x9762;&#x6570;&#x636E;&#x8986;&#x76D6;&#x524D;&#x9762;&#x7684;
    let obj = Object.assign({}, defaults, json);
    console.log(obj); // {name: &apos;liu&apos;, score: 100}
&lt;/script&gt;
</code></pre>
<pre><code class="lang-js">&lt;script type=&quot;text/javascript&quot;&gt;
    let arr = [&apos;apple&apos;, &apos;banana&apos;];
    let arr2 = Object.assign([], arr);
    arr.push(&apos;orange&apos;);
    console.log(arr); // [&apos;apple&apos;, &apos;banana&apos;, &apos;orange&apos;]
    console.log(arr2); // [&apos;apple&apos;, &apos;banana&apos;]
&lt;/script&gt;
</code></pre>
<h3 id="objectkeys-objectvalues-objectentries">Object.keys Object.values Object.entries</h3>
<pre><code class="lang-js">&lt;script type=&quot;text/javascript&quot;&gt;
    let json = {
        a:1,
        b:2,
        c:3
    };

    for (let key of Object.keys(json)) {
        console.log(key); // a b c
    }

    for (let value of Object.values(json)) {
        console.log(value); // 1 2 3
    }

    for (let [key, value] of Object.entries(json)) {
        console.log(key, &quot; --&gt; &quot;, value);
        /**
        a  --&gt;  1
        b  --&gt;  2
        c  --&gt;  3
        */
    }

    // &#x4F7F;&#x7528;&#x89E3;&#x6784;
    // &#x8FD9;&#x6837;&#x7684;&#x8BDD;, keys&#x4EE3;&#x8868;Object.keys, values&#x4EE3;&#x8868;Object.values...
    let {keys, values, entries} = Object; 
    for (let key of keys(json)) {
        console.log(key); // a b c
    }
    for (let value of values(json)) {
        console.log(value); // 1 2 3
    }
    for (let [key, value] of entries(json)) {
        console.log(key, &quot; --&gt; &quot;, value);
        /**
        a  --&gt;  1
        b  --&gt;  2
        c  --&gt;  3
        */
    }
&lt;/script&gt;
</code></pre>
<h3 id="&#x5BF9;&#x8C61;">...&#x5BF9;&#x8C61;</h3>
<pre><code class="lang-js">&lt;script type=&quot;text/javascript&quot;&gt;
    //&#x5BF9;&#x8C61;&#x7684;&#x6269;&#x5C55;&#x8FD0;&#x7B97;&#x7B26;
    let json = {x:1, y:2, a:3, b:4};
    let {x, y, ...z} = json;
    console.log(x, y, z); // 1 2 {a: 3, b: 4}
    console.log(z.a, z.b); // 3 4
&lt;/script&gt;
</code></pre>
<pre><code class="lang-js">&lt;script type=&quot;text/javascript&quot;&gt;
    let json = {a:3, b:4};
    let json2 = {...json}; //&#x65B0;&#x5BF9;&#x8C61;
    delete json2.b;
    console.log(json2); // {a: 3}
    console.log(json); // {a: 3, b: 4} &#x53EF;&#x77E5;,&#x4E92;&#x4E0D;&#x5F71;&#x54CD;
&lt;/script&gt;
</code></pre>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="数组.html" class="navigation navigation-prev " aria-label="Previous page: 数组">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="prototype.html" class="navigation navigation-next " aria-label="Next page: prototype">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"对象","level":"1.7","depth":1,"next":{"title":"prototype","level":"1.8","depth":1,"path":"prototype.md","ref":"prototype.md","articles":[]},"previous":{"title":"数组","level":"1.6","depth":1,"path":"数组.md","ref":"数组.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":["livereload"],"pluginsConfig":{"livereload":{},"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"对象.md","mtime":"2021-12-05T14:35:46.120Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2022-02-17T15:54:39.156Z"},"basePath":".","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="gitbook/gitbook.js"></script>
    <script src="gitbook/theme.js"></script>
    
        
        <script src="gitbook/gitbook-plugin-livereload/plugin.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

